<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@include file="/WEB-INF/webpage/common/taglibs.jspf"%>
<!DOCTYPE html>
<html>
<head>
  <title>托运装车单冷链追溯</title>
  <meta name="decorator" content="list"/>
</head>
<body title="托运装车单冷链追溯">
<grid:grid id="ccZcdGridId" url="${adminPath}/cc/zcd/ajaxList" datatype="local" beforeInit="true" afterInit="true">
    <grid:column label="sys.common.opt"  name="opt" formatter="button" width="100" sortable="false"/>
    <grid:button title="装箱关联"  groupname="opt" function="rowDialogCommit" winheight="550px" outclass="btn-primary" url="${adminPath}/cc/zcd/mx?zhuangcdNo=\"+row.zhuangcdNo+\"" />
    <grid:column label="cc.zcdHz.zhuangcdNo" name="zhuangcdNo" query="true" condition="like"/>
    <grid:column label="cc.zcdHz.facTime" name="facTime" query="true" queryMode="date" condition="between" />
    <grid:column label="cc.zcdHz.psy" name="psy" />
    <grid:column label="cc.zcdHz.siji" name="siji" />
    <grid:column label="cc.zcdHz.chepaiNo" name="chepaiNo" />
    <grid:column label="cc.zcdHz.state" name="state" dict="zcdState" query="true" queryMode="select"/>
    <grid:column label="cc.zcdHz.tygs" name="tygs"/>
    <grid:column label="cc.zcdHz.tygs" name="tuoyCompany" dict="tuoyCompany" query="true" queryMode="select" hidden="true"/>
    <grid:column label="cc.zcdHz.backFlg" name="backFlg" dict="sf" query="true" queryMode="select"/>
    <grid:column label="cc.cangku" name="cangku" dict="cangku" query="true" queryMode="select"/>

    <grid:toolbar title="冷藏车关联" icon="fa-file-text-o" btnclass="btn-primary" onclick="carRelation()"/>
    <grid:toolbar title="装车确认" icon="fa-file-text-o" onclick="facConfirm()"/>
    <grid:toolbar title="签收确认" icon="fa-file-text-o" btnclass="btn-warning" onclick="handOver()"/>
    <grid:toolbar title="作废" icon="fa fa-trash-o" btnclass="btn-danger" onclick="cancel()"/>
    <grid:toolbar title="同步" icon="fa-file-text-o" btnclass="btn-primary" onclick="syncZcd()"/>
    <grid:toolbar title="装车启运信息反馈" icon="fa-file-text-o" btnclass="btn-primary" onclick="backZcd()"/>
	<grid:toolbar function="search"/>
	<grid:toolbar function="reset"/>
</grid:grid>

<script>

    function beforeInit() {
        let today = new Date();
        $("#facTime_date_from").val(today.getFullYear() + "-" + (today.getMonth()) + "-" + today.getDate());
        $("#facTime_date_to").val(today.getFullYear() + "-" + (today.getMonth() + 1) + "-" + today.getDate());
    }

    function afterInit() {
        let queryParams = {
            ownFlg: 'N',
            'query.ownFlg||eq': 'N',
            deliveryState: '2',
            'query.deliveryState||eq': '2'
        };
        let queryFields=$('#queryFields').val();
        queryParams['queryFields'] = queryFields;
        //普通的查询
        $('#ccZcdGridIdGridQuery').find(":input").each(function() {
            let val = $(this).val();
            if (queryParams[$(this).attr('name')]) {
                val = queryParams[$(this).attr('name')] + "," + $(this).val();
            }
            queryParams[$(this).attr('name')] = val;
        });

        // 普通的查询
        $('#ccZcdGridIdGridQuery').find(":input").each(function() {
            let condition = $(this).attr('condition');
            if (!condition) {
                condition = "";
            }
            let key = "query." + $(this).attr('name') + "||" + condition;
            queryParams[key] = queryParams[$(this).attr('name')];
        });

        $("#ccZcdGridIdGrid").jqGrid('setGridParam',{
            postData:queryParams, //发送数据
            datatype:'json',
            mtype:'get',
            url:'${adminPath}/cc/zcd/ajaxList?gridtype=jqgrid',
            prmNames:{
                //请求参数格式预处理
                page:"page.pn",
                rows:"page.size",
                sort:"sort",
                order: "order"
            }
        }).trigger('reloadGrid');
    }

    /**
     * 冷藏车关联
     */
    function carRelation() {
        let selectedRows = $("#ccZcdGridIdGrid").jqGrid('getGridParam','selarrrow');
        // 如果没有选择，提示
        if (selectedRows.length < 1) {
            top.layer.alert('请至少选择一条装车单进行冷藏车关联!', {icon: 0, title:'警告'});
            return;
        }
        // 获取数据
        let zcd;
        let zcdNos = [];
        for (let i = 0; i < selectedRows.length; i++) {
            zcd = $("#ccZcdGridIdGrid").jqGrid('getRowData', selectedRows[i]);
            // 如果装车单状态不为0:未关联 1:已关联，不允许关联冷藏车
            if (zcd.state != '0' && zcd.state != '1') {
                top.layer.alert('只有未关联或者已关联的装车单才可以进行冷藏车关联!', {icon: 0, title:'警告'});
                return;
            }
            zcdNos.push(zcd.zhuangcdNo);
        }

        // 打开交接页面，选择需要交接的单据以及交接时间
        openDialogCommit("冷藏车关联", "${adminPath}/cc/zcd/carRelationEdit?zhuangcdNos=" + zcdNos, "ccZcdGridIdGrid", "1000px", "500px");
    }

    /**
     * 装车确认
     */
    function facConfirm() {
        let selectedRows = $("#ccZcdGridIdGrid").jqGrid('getGridParam','selarrrow');
        let zcdNos = [];
        // 如果没有选择，提示
        if (selectedRows.length == 0) {
            top.layer.alert('请至少选择一条数据!', {icon: 0, title:'警告'});
            return;
        }
        // 获取装车单号
        for (let i = 0; i < selectedRows.length; i++) {
            if ($("#ccZcdGridIdGrid").jqGrid('getRowData', selectedRows[i]).state != '1') {
                top.layer.alert('只有已关联的装车单才可以进行发车!', {icon: 0, title:'警告'});
                return;
            }
            zcdNos.push($("#ccZcdGridIdGrid").jqGrid('getRowData', selectedRows[i]).zhuangcdNo);
        }

        // POST请求发车确认
        $.ajax({
            type: "POST",
            url: "${adminPath}/cc/zcd/facConfirm",
            data: {
                zcdNos : zcdNos
            },
            traditional: true,
            success: function (data) {
                if (data.ret == 0) {
                    top.layer.alert(data.msg, {icon: 0, title:'提示'});
                    //刷新表单
                    refreshTable("ccZcdGridIdGrid");
                }
                else {
                    top.layer.alert(data.msg, {icon: 0, title:'提示'});
                }
            }
        });
    }

    /**
     * 签收确认
     */
    function handOver() {
        let selectedRows = $("#ccZcdGridIdGrid").jqGrid('getGridParam','selarrrow');
        // 如果没有选择，提示
        if (selectedRows.length != 1) {
            top.layer.alert('请选择一条装车单进行配送交接!', {icon: 0, title:'警告'});
            return;
        }
        // 如果装车单状态不为2:已激活，不允许交接
        let zcd = $("#ccZcdGridIdGrid").jqGrid('getRowData', selectedRows[0]);
        if (zcd.state != '2') {
            top.layer.alert('只有激活关联的装车单才可以进行交接!', {icon: 0, title:'警告'});
            return;
        }

        // 打开交接页面，选择需要交接的单据以及交接时间
        openDialogCommit("配送交接", "${adminPath}/cc/zcd/handOverEdit?zhuangcdNo=" + zcd.zhuangcdNo, "ccZcdGridIdGrid", "1000px", "500px");
    }

    /**
     * 单据作废
     */
    function cancel() {

        let selectedRows = $("#ccZcdGridIdGrid").jqGrid('getGridParam','selarrrow');
        let zcdNos = [];
        // 如果没有选择，提示
        if (selectedRows.length == 0) {
            top.layer.alert('请至少选择一条数据!', {icon: 0, title:'警告'});
            return;
        }
        // 获取装车单号
        for (let i = 0; i < selectedRows.length; i++) {
            if ($("#ccZcdGridIdGrid").jqGrid('getRowData', selectedRows[i]).state != '0') {
                top.layer.alert('只有未关联的装车单才可以作废!', {icon: 0, title:'警告'});
                return;
            }
            zcdNos.push($("#ccZcdGridIdGrid").jqGrid('getRowData', selectedRows[i]).zhuangcdNo);
        }

        // POST请求发车确认
        $.ajax({
            type: "POST",
            url: "${adminPath}/cc/zcd/cancel",
            data: {
                zcdNos : zcdNos
            },
            traditional: true,
            success: function (data) {
                if (data.ret == 0) {
                    top.layer.alert(data.msg, {icon: 0, title:'提示'});
                    //刷新表单
                    refreshTable("ccZcdGridIdGrid");
                }
                else {
                    top.layer.alert(data.msg, {icon: 0, title:'提示'});
                }
            }
        });
    }

    /**
     * 同步装车单
     */
    function syncZcd() {
        let zcdNo = $("#ccZcdGridIdGridQuery").find("[name='zhuangcdNo']").val();

        if (isNull(zcdNo)) {
            top.layer.alert("请输入装车单号后同步！", {icon: 0, title:'提示'});
            return;
        }

        // POST请求同步装车单
        $.ajax({
            type: "POST",
            url: "${adminPath}/cc/zcd/syncZcd",
            data: {
                zcdNo : zcdNo
            },
            traditional: true,
            success: function (data) {
                if (data.ret == 0) {
                    top.layer.alert(data.msg, {icon: 0, title:'提示'});
                    //刷新表单
                    refreshTable("ccZcdGridIdGrid");
                }
                else {
                    top.layer.alert(data.msg, {icon: 0, title:'提示'});
                }
            }
        });
    }

    /**
     * 装车启运信息反馈
     */
    function backZcd() {

        let selectedRows = $("#ccZcdGridIdGrid").jqGrid('getGridParam','selarrrow');
        let zcdNos = [];
        // 如果没有选择，提示
        if (selectedRows.length == 0) {
            top.layer.alert('请至少选择一条数据!', {icon: 0, title:'警告'});
            return;
        }
        // 获取装车单号
        for (let i = 0; i < selectedRows.length; i++) {
            if ($("#ccZcdGridIdGrid").jqGrid('getRowData', selectedRows[i]).state < '2') {
                top.layer.alert('只有已激活或已完成的装车单才可以反馈!', {icon: 0, title:'警告'});
                return;
            }
            zcdNos.push($("#ccZcdGridIdGrid").jqGrid('getRowData', selectedRows[i]).zhuangcdNo);
        }

        // POST请求装车启运信息反馈
        $.ajax({
            type: "POST",
            url: "${adminPath}/cc/zcd/back",
            data: {
                zcdNos : zcdNos
            },
            traditional: true,
            success: function (data) {
                if (data.ret == 0) {
                    top.layer.alert(data.msg, {icon: 0, title:'提示'});
                    //刷新表单
                    refreshTable("ccZcdGridIdGrid");
                }
                else {
                    top.layer.alert(data.msg, {icon: 0, title:'提示'});
                }
            }
        });
    }

</script>

</body>
</html>